<?php //if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller {

  public function __construct()
  {
    parent::__construct();
    $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
  }

  public function register()
  {
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.user_mail]');
    $this->form_validation->set_rules('password', 'Password', 'required');
    if ($this->form_validation->run() == FALSE)
      {
	  $this->showLogin();
      }
    else
      {
	$this->user_model->user_mail = $this->input->post('email'); 
	$this->user_model->user_password = $this->input->post('password'); 
	$this->user_model->insert();
	
	if ($this->user_model->checkLogin($this->input->post('email'), 
					  $this->input->post('password')))
	  {
	    $newdata = array(
			     'user_id'  => $this->user_model->user_id,
			     'user_mail' => $this->user_model->user_mail
			     );

	    $this->session->set_userdata($newdata);
	  }
      }
    $this->index();
  }

  public function logout()
  {
    $this->session->unset_userdata('user_id');
    $this->session->unset_userdata('user_mail');
    $this->index();
  }
  
  public function callbackLogin($a)
  {
    if ($this->user_model->checkLogin($this->input->post('email'), 
					  $this->input->post('password')))
	  {
	    $newdata = array(
			     'user_id'  => $this->user_model->user_id,
			     'user_mail' => $this->user_model->user_mail
			     );

	    $this->session->set_userdata($newdata);
	    return true;
	  }
      $this->form_validation->set_message('callbackLogin', 'Wrong email or pass');
      return false;
  }
  
  public function login()
  {
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required');
    $this->form_validation->set_rules('password', 'Password', 'callback_callbackLogin');
    if ($this->form_validation->run() == FALSE)
      {
	$this->showLogin();
      }
    $this->index();
  }

  public function redirect()
  {
      $this->drawPage($this->parser->parse('home_redirect', $this->putTemplateArgs(array()), true));
  }

  public function home()
  {
      $this->drawPage($this->parser->parse('home', $this->putTemplateArgs(array()), true));
  }
  
  public function start()
  { 
	$this->drawPage($this->parser->parse('start', $this->putTemplateArgs(array()), false));
}
   
  public function index()
  {
    (!$this->session->userdata('user_id')) ? 
      $this->drawPage($this->parser->parse('home', $this->putTemplateArgs(array()), true)) : 
      $this->drawPage($this->parser->parse('seller_links', $this->putTemplateArgs(array()), true));
  }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */